package ui;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class ScreenPainter {

    private Graphics graphics;
    private int width;
    private int height;
    private int halfWidth;
    private int halfHeight;
    private int dx = 0;
    private int dy = 0;
    private final int delta = 25;

    public ScreenPainter(int width, int height) throws IOException {
        this.width = width;
        this.height = height;
        this.halfHeight = height / 2;
        this.halfWidth = width / 2;
    }
    private final int UP = -1;
    private final int DOWN = -2;
    private final int LEFT = -3;
    private final int RIGHT = -4;
    private final int OK = -5;

    public void Paint(Graphics g, Image image, int movement) throws IOException {
        this.graphics = g;
        int halfImgWidth = image.getWidth() / 2;
        int halfImgHeight = image.getHeight() / 2;
        switch (movement) {
            case UP:
                if ((dy + delta) <= (halfImgHeight - halfHeight)) {
                    dy += delta;
                }
                break;
            case DOWN:
                if ((dy - delta) >= -(halfImgHeight - halfHeight)) {
                    dy -= delta;
                }
                break;
            case LEFT:
                if ((dx + delta) <= (halfImgWidth - halfWidth)) {
                    dx += delta;
                }
                break;
            case RIGHT:
                if ((dx - delta) >= -(halfImgWidth - halfWidth)) {
                    dx -= delta;
                }
                break;
        }
        drawImage(image);
    }

    private void drawImage(Image image) {
        graphics.drawImage(image, halfWidth + dx, halfHeight + dy, Graphics.VCENTER | Graphics.HCENTER);
        
    }
}
